//
//  CarInfoViewController.swift
//  Automobile
//
//  Created by erkebulan elzhan on 9/3/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit


final class CarInfoViewController: UIViewController, Alertable {
    
    // MARK: - Outlets
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.register(CarInfoFullCell.self, forCellReuseIdentifier: "CarInfoFullCell")
        tableView.register(CarGeneralInfoCell.self, forCellReuseIdentifier: "CarGeneralInfoCell")
        tableView.separatorStyle = .none
        tableView.tableFooterView = UIView()
        tableView.backgroundColor = .lightBlueColor
        tableView.delegate = self
        tableView.dataSource = self
        tableView.showsVerticalScrollIndicator = false
        return tableView
    }()
    
    private var spinnerView = SpinnerView()
    
    // MARK: - Props
    
    private lazy var viewModel: CarInfoViewModel = {
        let viewModel = CarInfoViewModel()
        viewModel.delegate = self
        return viewModel
    }()
    
    // MARK: - Actions
    
    @objc private func goBack (sender: UIBarButtonItem) {
        self.navigationController?.popViewController(animated: true)
    }
    
    public func setProps (post: Post) {
        viewModel.post = post
        viewModel.setUpCells()
    }
    
    // MARK: - Init
    
    override func viewDidLoad() {
        self.addViews()
        viewModel.loadData()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        self.setUpNavBar()
    }
}


extension CarInfoViewController: ViewModelProtocol {
    func updateCities(cities: [City]?) {}
    
    func updateSpinner(showSpinner: Bool) {
        showSpinner ? spinnerView.startAnimating() : spinnerView.stopAnimating()
        view.isUserInteractionEnabled = !showSpinner
    }
    
    func didHappenError(error: RequestError) {
        alertError(error: error)
    }
    
    func updatePosts() {
        tableView.reloadData()
    }
}


extension CarInfoViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let item = viewModel.items[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: type(of: item).reuseId)!
        item.configure(cell: cell, index: indexPath.row)
        cell.selectionStyle = .none
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.items.count
    }
}


extension CarInfoViewController {
    
    private func addViews () {
        view.addSubview(tableView)
        view.addSubview(spinnerView)
        addConstraints()
    }
    
    private func addConstraints () {
        tableView.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(view.safeAreaInsets)
            make.leading.trailing.equalToSuperview()
        }
        
        spinnerView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
    }
    
    private func setUpNavBar() {
        self.navigationItem.title = "Характеристики"
        self.addBackIcon()
    }
    
    private func addBackIcon () {
        let backIcon = UIImage(named: "cancel")
        let backButton = UIBarButtonItem(image: backIcon, style: .plain, target: self, action: #selector(goBack(sender:)))
        backButton.tintColor = .greyColor
        navigationItem.leftBarButtonItem = backButton
    }
}
